#ifndef xpack_test_lang_cxx_index_of_first
#define xpack_test_lang_cxx_index_of_first
#pragma push_macro("xuser")
#undef  xuser
#define xuser mixc::test_lang_cxx_index_of_first::inc
#include"test/helper.hpp" // 需要放第一
#include"lang/cxx/ignore_case.hpp"
#include"lang/cxx/index_of_first.hpp"
#include"lang/cxx.hpp"
#include<string.h>
#pragma pop_macro("xuser")

namespace mixc::test_lang_cxx_index_of_first{
    xtest("index_of_first"){
        #define xgen(INDEX,VAL,...)                                 \
        {                                                           \
            inc::c08 value = VAL;                                   \
            xassert_eq(value.index_of_first(__VA_ARGS__), INDEX);   \
        }

        xgen(not_exist, "", "");
        xgen(not_exist, "123", "");
        xgen(0u, "123", '1');
        xgen(1u, "123", '2');
        xgen(2u, "123", '3');
        xgen(not_exist, "123", '4');
        xgen(0u, "123", "1");
        xgen(1u, "123", "2");
        xgen(2u, "123", "3");
        xgen(1u, "121", "21"); // 回溯测试
        xgen(not_exist, "123", "4");


        // 回溯测试
        enum{
            max_length = 10, // 10 个长度的字符
            max_deepth = 2, // 2bit deepth，匹配字符串最多同时拥有 4 个不一样的字符
            max_case = 1 << (max_length * max_deepth),
            max_match_str_length = 5,
        };

        char gen_main[max_length + 1] = {};
        char gen_match[max_length + 1] = {};

        for(uxx match_length = 2; match_length < max_match_str_length; match_length++){
            for(uxx main_length = match_length; main_length < max_length; main_length++){
                for(uxx j = 0, t = match_length; j < main_length; j++){
                    gen_match[j] = 'a' + t % 2;
                    t /= 2;
                }

                gen_match[match_length] = '\0';

                for(uxx i = 0, end = 1 << (main_length * max_deepth); i < end; i++){
                    for(uxx j = 0, t = i; j < main_length; j++){
                        gen_main[j] = 'a' + t % 2;
                        t /= 2;
                    }

                    auto main_str = inc::c08(gen_main, main_length);
                    auto match_str = inc::c08(gen_match, match_length);
                    auto p_result = strstr(gen_main, gen_match);
                    auto i_result = p_result ? p_result - gen_main : not_exist;
                    xassert_eq(main_str.index_of_first(match_str), i_result, main_str, match_str);
                }
            }
        }

        xgen(1u, "abcd", 'b', inc::ignore_case<char>);
        xgen(1u, "abcd", 'B', inc::ignore_case<char>);

        xgen(1u, "abcd", "b", inc::ignore_case<char>);
        xgen(1u, "abcd", "B", inc::ignore_case<char>);

        xgen(1u, "abcd", "bC", inc::ignore_case<char>);
        xgen(1u, "abcd", "Bc", inc::ignore_case<char>);

        xgen(4u, "123aBcd", "bC", inc::ignore_case<char>);
        xgen(4u, "123abCd", "Bc", inc::ignore_case<char>);

        #undef  xgen
        #define xgen(MAIN,PATTERN,VAL,...)                          \
        {                                                           \
            inc::c08 value = VAL;                                   \
            auto result = value.index_of_first(__VA_ARGS__);        \
            xassert_eq(result->i_main, MAIN);                       \
            xassert_eq(result->i_pattern, PATTERN);                 \
        }

        xgen(2u, 1u, "1234", { '5', '3' });
        xgen(1u, 0u, "1234", { '2', '3' });
        xgen(not_exist, not_exist, "1234", { '5', '6' });
        #undef xgen
    };
}

#endif
